﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MiniExcelLibs.Attributes;

namespace thinger.MTHModels
{
    /// <summary>
    /// 设备配置
    /// </summary>
    public class Device
    {
        /// <summary>
        /// 报警处理事件
        /// </summary>
        public event Action<bool, Varlable> AlarmEvent;
        /// <summary>
        /// 应用配方名称
        /// </summary>
        public string RecipeName { get; set; }

        /// <summary>
        /// 通信ip
        /// </summary>
        public string IPAdress { get; set; }
        /// <summary>
        /// 端口号
        /// </summary>
        public int Port { get; set; }
        /// <summary>
        /// 通信组
        /// </summary>
        [ExcelIgnore]
        public List<Group> Groups { get; set; }
        /// <summary>
        /// 是否连接标签
        /// </summary>
        [ExcelIgnore]
        public bool IsConnected { get; set; } = false;
        /// <summary>
        /// 重连标志
        /// </summary>
        [ExcelIgnore]
        public bool ReConncentSign { get; set; } = false;
        /// <summary>
        /// 重连时间
        /// </summary>
        [ExcelIgnore]
        public int ReConnectTime { get; set; } = 1000;
        /// <summary>
        /// varName的字典
        /// </summary>
        [ExcelIgnore]
        public Dictionary<string, object> DictionaryVarValue { get; set; } = new Dictionary<string, object>();
        /// <summary>
        /// 更新变量
        /// </summary>
        /// <param name="varlable"></param>
        public void UpDataVarValue(Varlable varlable)
        {
            if (DictionaryVarValue.ContainsKey(varlable.VarName))
            {
                DictionaryVarValue[varlable.VarName] = varlable.VarValue;
            }
            else
            {
                DictionaryVarValue.Add(varlable.VarName, varlable.VarValue);
            }
            CheckAlarm(varlable);
        }
        /// <summary>
        /// 报警
        /// </summary>
        /// <param name="varlable"></param>
        private void CheckAlarm(Varlable varlable)
        {
            //上升沿报警
            if (varlable.PosAlarm)
            {
                if (varlable.VarValue.ToString().ToLower().Trim() == "true" && varlable.PosAlarmCache.ToString().Trim().ToLower() == "false")
                {
                    //上升沿报警事件
                    AlarmEvent(true, varlable);
                }
                varlable.PosAlarmCache = (bool)varlable.VarValue;
            }
            //下降延报警
            if (varlable.NegAlarm)
            {
                if (varlable.VarValue.ToString().ToLower().Trim() == "false" && varlable.NegAlarmCache.ToString().Trim().ToLower() == "true")
                {
                    //下降沿报警事件
                    AlarmEvent(true, varlable);
                }
                varlable.NegAlarmCache = (bool)varlable.VarValue;
            }
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object this[string key] 
        {
            get
            {
                return DictionaryVarValue[key];
            }
        }
    }
}
